<template>
  <div>
    <el-dialog 
    :title="addInfo.isAdd ? '添加商品分类' : '编辑商品分类'" 
    :visible.sync="addInfo.isShow" 
    :before-close="cancel" 
    center>
      <el-form :model="cateForm" :rules="rules" ref="userinfo">
        <el-form-item label="上级分类" :label-width="formLabelWidth" prop="pid">
          <el-select  v-model="cateForm.pid" placeholder="请选择上级分类">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option 
            v-for="item in catelist" 
            :key="item.id" 
            :label="item.catename" 
            :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" :label-width="formLabelWidth" prop="catename">
          <el-input autocomplete="off" v-model="cateForm.catename" ></el-input>
        </el-form-item>
        <el-form-item label="图片" :label-width="formLabelWidth"
          v-if="cateForm.pid != 0"
        >
          <el-upload
            action="#"
            list-type="picture-card"
            :auto-upload="false"
            :on-preview="onPreview"
            
            :on-change="onChange"
            :on-exceed="onExceed"
            :file-list="fileList"
            :limit="1"
            >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch 
          v-model="cateForm.status" 
          active-color="#13ce66" 
          inactive-color="#ff4949" 
          :active-value="1"
          :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="confirn" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapGetters } from 'vuex';
import { cateadd, cateinfo, cateedit } from '../../request/api'
export default {
  props: ['addInfo'],
  data() {
    return {
      formLabelWidth: '120px',
      cateForm: {
        pid:0,//上级分类编号
        catename: "", //商品分类名称
        img: "", //图片(文件，一般用于二级分类)
        status: 1, //状态   1正常2禁用   number类型number
      },
      rules: {
        catename: [
          { required: true, message: '请输入商品分类名称', trigger: 'blur' },
          { min: 2, max: 11, message: '长度在 2 到 11 个字符', trigger: 'blur' }
        ],
        pid: [
          { required: true, message: '请输入上级分类', trigger: 'change' }  
        ]
      },
      dialogImageUrl:'',//图片地址
      dialogVisible:false,//控制图片预览的显示隐藏
      fileList:[]//文件列表
    }
  },
  mounted() {
    
  },
  computed:{
    ...mapGetters({
      catelist:'cate/getCate'
    })
  },
  methods: {
    ...mapActions({
      getcateAction:'cate/getcateAction'
    }),
    reset() {
      this.cateForm = {
        pid:0,//上级分类编号
        catename: "", //商品分类名称
        img: "", //图片(文件，一般用于二级分类)
        status: 1, //状态   1正常2禁用   number类型number
      };
      this.$refs.userinfo.resetFields();//重置表单验证
      //清空上传文件
      this.fileList=[]
    },
    //取消事件
    cancel() {
      this.$emit('cancel', false);
      //清空
      this.reset();
    },
    //数据回显
    lookup(id) {
      cateinfo({ id }).then((res) => {
        // console.log(res);
        if (res.code == 200) {
          this.cateForm = res.list;
          this.cateForm.id = id;
          //对图片进行单独配置
          this.fileList = this.cateForm.img ? [{url:'http://localhost:3000'+this.cateForm.img}] : []
        }
      })
    },
    //确定事件
    confirn() {
      // console.log(this.cateForm);
      this.$refs.userinfo.validate((val) => {
        if (val) {
          if (this.addInfo.isAdd) {
            //添加逻辑
            cateadd(this.cateForm).then((res) => {
              // console.log(res);
              if (res.code == 200) {
                //添加成功
                this.$message.success(res.msg);
                //重新获取列表
                this.getcateAction();
                //关闭弹窗
                this.cancel()
              } else {
                this.$message.error(res.msg)
              }
            })
          }else{
            //编辑逻辑
            cateedit(this.cateForm).then((res)=>{
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                //重新获取列表
                this.getcateAction();
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        }
      })
    },
    //图片预览
    onPreview(file){
      console.log(file,'文件预览');
      //显示弹框
      this.dialogVisible = true;
      this.dialogImageUrl = file.url
    },
    //图片移除
    
    //图片改变时发生的变化
    onChange(file, fileList){
      console.log(file,'文件');
      console.log(fileList,'文件列表');
      this.cateForm.img = file.raw
    },
    //超出文件上传的回调函数
    onExceed(files, fileList){
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    }
  },
};
</script>

<style scoped>

</style>
